<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/2/25
 * Time: 9:16
 */

namespace app\login\controller;


use app\api\service\Token;
use app\login\service\identificationService;
use app\login\validate\IdentificationData;
use think\facade\Request;

class Identification extends BaseController
{
    public function index()
    {
        return $this->fetch('/login/Identification');
    }
    # 添加标识码和IP放行规则接口
    public function identificationAndIpApi()
    {
        (new IdentificationData())->goCheck();
        $name = Token::getRedisTokenName();
        $arr = Request::post();
        $service = new identificationService();
        if(key_exists('host', $arr) == false){
            $arr['host'] = null;
        }
        $result = $service->addIdentificationOrIp($arr['type'], $name, $arr['data'], $arr['host']);
        if(!$result){
            return json([
               'code' => '400'
            ]);
        }else{
            return json([
                'code' => '200'
            ]);
        }
    }

    # 初始化标识码和IP
    public function Initialization($type)
    {
        if($type == 'sign'){
            (new IdentificationData()) ->goCheck();
        }
        $name = Token::getRedisTokenName();
        $service = new identificationService();
        $result = $service->initializationData($name, $type);
        if(!$result){
            return json([
                'code' => '400'
            ]);
        }else{
            return json([
                'data' => $result
            ]);
        }
    }

    # 获取所有标识码并组成对应的网站
    public function allUrl()
    {
        $name = Token::getRedisTokenName();
        $service = new identificationService();
        $result = $service->returnAllUrl($name);
        return json([
           'code' => 0,
           'data' => $result
        ]);
    }
    # 删除标识码或者IP黑名单
    public function delSignOrIp($data,$type)
    {
        (new IdentificationData())->goCheck();
        $name = Token::getRedisTokenName();
        $service = new identificationService();
        $service->delSignOrIP($data,$name,$type);
        return json([
           'code' => 200
        ]);
    }

}